package org.ayachinene.leetcode.problems;

public class P1664 {

    static class Solution {

        public int waysToMakeFair(int[] nums) {
            int preOddSum = 0;
            int preEvenSum = 0;
            int afterOddSum = 0;
            int afterEvenSum = 0;
            for (int i = 0; i < nums.length; ++i) {
                // 奇数
                if ((i & 1) != 0) {
                    afterOddSum += nums[i];
                } else {
                    afterEvenSum += nums[i];
                }
            }

            int res = 0;
            for (int i = 0; i < nums.length; ++i) {
                if ((i & 1) != 0) {
                    afterOddSum -= nums[i];
                } else {
                    afterEvenSum -= nums[i];
                }
                if (preOddSum + afterEvenSum == preEvenSum + afterOddSum) {
                    res++;
                }
                if ((i & 1) != 0) {
                    preOddSum += nums[i];
                } else {
                    preEvenSum += nums[i];
                }
            }
            return res;
        }
    }
}
